package com.tansun.easycare.rule.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.tansun.easycare.rule.common.jfun.JFun;
import com.tansun.easycare.rule.common.jfun.JFunParam;
import com.tansun.rule.utils.StringUtils;

/**
 * 用于规则集函数解析，除此之外还有ParamParse,EquationParse
 * @author xuconghui
 *
 */
public class FuncParse{
	
	private boolean isFuncRuning = false;//当前是否为函数，默认fasle
	
	private String funcName;//函数名称,函数名类似 StringTypeRuleUtils.getIndexString( 位于jfun.xml
	
	private String returnType;//返回值类型
	
	private String waitLine;//函数内容，等待
	
	private List<String> paramList = new ArrayList<String>();//参数

	private String currentParam;
	
	private Map<String, JFunParam> paramMap;//基础参数
	
	
	public FuncParse() {
		super();
		this.isFuncRuning = false;
	}

	public FuncParse(JFun fun) {
		super();
		this.isFuncRuning = true;
		this.funcName = fun.getDrlName();
		this.paramMap = fun.getParamMap();
		for(String param:fun.sortParamName()) {
			if( StringUtils.isEmpty(param) ){
				break;
			}else {
				this.paramList.add(param);
			}
		}
    	this.returnType = fun.getDataType();
	}

	@Override
	public String toString() {
		if( StringUtils.isNotEmpty(funcName) ) {
			return funcName +  StringUtils.join(paramList, ", ") + ")";
		}else {
			return "";
		}
	}

	public String getFuncName() {
		return funcName;
	}

	public void setFuncName(String funcName) {
		this.funcName = funcName;
	}

	public List<String> getParamList() {
		return paramList;
	}

	public void setParamList(List<String> paramList) {
		this.paramList = paramList;
	}
	
	public void addParam(String param) {
		this.paramList.add(param);
	}

	public String getWaitLine() {
		return waitLine;
	}

	public void setWaitLine(String waitLine) {
		this.waitLine = waitLine;
	}

	public String getCurrentParam() {
		return currentParam;
	}

	public void setCurrentParam(String currentParam) {
		if( !paramMap.containsKey(currentParam) ) {
			this.paramList.add(currentParam);
		}
		this.currentParam = currentParam;
	}
	
	public boolean hasCurrentParam() {
		return StringUtils.isNotEmpty(this.currentParam);
	}
	
	public void setRealParam(String realParam) {
		if(StringUtils.isNotEmpty(currentParam)) {
			int i = this.paramList.indexOf(currentParam);
			if(i!=-1) {
		        this.paramList.remove(currentParam);
		        this.paramList.add(i,realParam);
			}
			this.currentParam = null;
		}
	}

	public boolean isFuncRuning() {
		return isFuncRuning;
	}

	public void setFuncRuning(boolean isFuncRuning) {
		this.isFuncRuning = isFuncRuning;
	}

	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}
	
}
